﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Collections.Specialized;
using System.IO;

namespace NFinal
{
    //Controller基类
    public class BaseAction
    {
        public HttpContext context = null;
        public NameValueCollection get = null;
        public TextWriter tw = null;
        public LitJson.JsonData data = null;
        public BaseAction() { }
        public string app = "App";
        public string MapPath(string url)
        {
            return new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory).Parent.FullName + "\\" + url.Trim('/').Replace('/', '\\');
        }
        public BaseAction(string fileName)
        {
            System.IO.StreamWriter sw = new System.IO.StreamWriter(MapPath(fileName), false, System.Text.Encoding.UTF8);
            this.tw = sw;
        }
        public BaseAction(System.IO.TextWriter tw)
        {
            this.tw = tw;
        }
        protected void Write(string val)
        {
            tw.Write(val);
        }
        protected void Write(int val)
        {
            tw.Write(val);
        }

        protected string GetCookie(string key)
        { 
            return  context.Request.Cookies[key].Value;
        }
        protected void SetCookie(string key, string value)
        { 
            HttpCookie cookie=new HttpCookie(key,value);
            context.Response.Cookies.Add(cookie);
        }
        protected void ClearCookie(string key)
        {
            context.Response.Cookies.Remove(key);
        }

        public void AjaxReturn(LitJson.JsonData data)
        {
            AjaxReturn(data, 1, "");
        }
        public void AjaxReturn(string json)
        {
            AjaxReturn(json, 1, "");
        }
        public void AjaxReturn(LitJson.JsonData data,int code)
        {
            AjaxReturn(data, code, "");
        }
        public void AjaxReturn(string json, int code)
        {
            AjaxReturn(json, code, "");
        }
        public void AjaxReturn(LitJson.JsonData data, int code, string msg)
        {
            this.context.Response.ContentType = "application/json";
            this.context.Response.Write(string.Format("{{\"code\":{0},msg:\"{1}\",result:{2}}}",code,msg,data.ToJson()));
        }
        public void AjaxReturn(string json, int code, string msg)
        {
            this.context.Response.ContentType = "application/json";
            this.context.Response.Write(string.Format("{{\"code\":{0},msg:\"{1}\",result:\"{2}\"}}", code, msg, json));
        }
        public void AjaxReturn(int code, string msg)
        {
            this.context.Response.ContentType = "application/json";
            this.context.Response.Write(string.Format("{{\"code\":{0},msg:\"{1}\"}}", code, msg));
        }
        //魔法函数
        public static void View(string tplPath)
        { 
            
        }
        public static void View(string name, string tplPath)
        { 
            
        }
    }
}